<?php 

/**
 * Ajax_DeleteEvent.phtml - Suppression d'un evenement ou d'une liste des évènements
 *
 * @package
 * @author Alexis Eric
 * @copyright Copyright (c) 2012 - Erra Prod
 * @version $1.0$
 * @access public
 */
if (isset($_POST['DELETE_EVENT']) && is_array($_POST['DELETE_EVENT'])) {
    foreach ($_POST['DELETE_EVENT'] as $deleteData) {
        delete_event($deleteData);
    }
}

/**
 * delete_event() - Suppression d'un évènement
 * 
 * @param type $calEvent
 */
function delete_event($calEvent) {
    $occurency_to_delete = '';
    if ($calEvent['occurency'] != 'original') {
        if ($calEvent['slots_to_delete'] == 'only_this') {
            $occurency_to_delete = $calEvent['occurency'] . ';';
        } else if ($calEvent['slots_to_delete'] == 'this_and_further') {
            $occurency_to_delete = $calEvent['occurency'] . ';n;';
        }
    }
    $object_id = $calEvent['id'];
    $result = Erra_Core_DatabasePdo::SELECT('SELECT event_type FROM msa_slotobject WHERE object_id="' . $object_id . '"');
    if (Erra_Core_DatabasePdo::rowCount()) {
        // Envoyer un mail si nécessaire
        $row = $result->fetch();
        if ($row->event_type == "presta") {
            $result = Erra_Core_DatabasePdo::SELECT('SELECT from_internet, msa_agendaobject.telephone_portable AS presta_portable, msa_agendaobject.email AS presta_email, event_type, agenda_id, slot_day, slot_start, nom, prenom, msa_clientobject.telephone_portable, msa_clientobject.email FROM msa_slotobject, msa_clientobject, msa_agendaobject WHERE msa_slotobject.object_id="' . $object_id . '" AND msa_slotobject.client_id=msa_clientobject.id AND msa_agendaobject.id=msa_slotobject.agenda_id');
            if (Erra_Core_DatabasePdo::rowCount()) {
                $row = $result->fetch();
                // Send mail to client
                //////////////////////
                $shopName = Config_GeneralConfigObject::get('nom_etablissement');
                $date_reservation = date('d/m/Y', $row->slot_day);
                $heure_reservation = time_to_str_hour($row->slot_start);
                $placeHolders = array('{nom_etablissement}' => $shopName,
                    '{nom_complet}' => $row->nom . ' ' . $row->prenom,
                    '{date_reservation}' => $date_reservation,
                    '{heure_reservation}' => $heure_reservation);
                $template = (isset($calEvent['source']) && $calEvent['source'] == 'internal') ? 'Mail/Mail_RdvCancelInternal.phtml' : 'Mail/Mail_RdvCancel.phtml';
                Erra_Utils_Tools::sendTemplatedMail($row->email, $template, $placeHolders);

                // Send SMS only if the cancelation is not from the client himself
                if (isset($calEvent['source']) && $calEvent['source'] != 'client') {
                    if (Config_GeneralConfigObject::get('sms_client_deleterdv') == 1) {
                        Erra_Utils_Tools::sendTemplatedSms($row->telephone_portable, 'Sms/Sms_RdvCancelUser.phtml', $placeHolders);
                    }
                }
                // Send mail/sms to presta
                //////////////////////
                if (Config_GeneralConfigObject::get('mail_team_deleterdv') == 1) {
                    Erra_Utils_Tools::sendTemplatedMail($row->presta_email, 'Mail/Mail_RdvCancelPresta.phtml', $placeHolders);
                }
                // Send SMS only if the cancelation is not from the client himself
                if (Config_GeneralConfigObject::get('sms_team_deleterdv') == 1 && !(Config_GeneralConfigObject::get('sms_team_internetonly') && !$rdv->from_internet)) {
                    Erra_Utils_Tools::sendTemplatedSms($row->presta_portable, 'Sms/Sms_RdvCancelPresta.phtml', $placeHolders);
                }
            }
        }
        if ($row->event_type != "presta" && strlen($occurency_to_delete)) {
            Erra_Core_DatabasePdo::EXECUTE('UPDATE msa_slotobject SET deleted_occurencies = CONCAT("' . $occurency_to_delete . '", deleted_occurencies) WHERE object_id="' . $object_id . '"');
        } else {
            // Copy to trash
            if ($row->event_type == 'presta') {
                Erra_Core_DatabasePdo::EXECUTE('INSERT INTO msa_slotobjecttrash (SELECT * FROM msa_slotobject WHERE object_id="' . $object_id . '")');
                Erra_Core_DatabasePdo::EXECUTE('UPDATE msa_slotobjecttrash SET object_class="Msa_SlotObjectTrash" WHERE object_id="' . $object_id . '"');
            }
            Erra_Core_DatabasePdo::EXECUTE('DELETE FROM msa_slotobject WHERE object_id="' . $object_id . '"');

            // Set here the informations for agenda syncing
            Erra_Core_DatabasePdo::EXECUTE('INSERT INTO agenda_sync(action, event_id, event_type, agenda_id, last_sync) VALUES("delete", "' . $object_id . '", "' . $calEvent['event_type'] . '", ' . $calEvent['agenda_id'] . ', ' . time() . ');');
            // Delete event on google calendar if exists
            Tools_GCalendarObject::deleteEventFromUniqid($object_id);
        }
    }
}